package com.hongfeng.support.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hongfeng.support.item.ParmItem;
import com.hongfeng.support.item.QueryItem;
import com.hongfeng.support.item.tag.FieldOperator;
import com.hongfeng.support.model.AutoType;
import com.hongfeng.support.model.Product;
import com.hongfeng.support.service.AutoTypeService;
import com.hongfeng.support.service.ProductService;
import com.hongfeng.support.service.common.impl.GenericServiceImpl;

@Service
public class AutoTypeServiceImpl extends GenericServiceImpl<AutoType, Long>
		implements AutoTypeService {

	@Resource
	private ProductService productService;

	public AutoTypeServiceImpl() {
		super(AutoType.class);
	}

	@Override
	public void delete(Long id) {
		ParmItem item = new ParmItem("autoType.id", id, FieldOperator.EQUAL);
		List<Product> products = productService.findAllByQuery(Product.class,
				item);
		for (Product product : products) {
			product.setAutoType(null);
			productService.update(product);
		}
		super.delete(id);
	}

	public List<AutoType> findByBrandId(Long brandId) {
		QueryItem item = new QueryItem(AutoType.class);
		if (brandId != null) {
			item.addCondition("autoBrand.id", FieldOperator.EQUAL,
					brandId.longValue());
		}
		return findAll(item);
	}

}
